package pers.ht.mbpo.service;

import net.sf.json.JSONArray;
import pers.ht.mbpo.bean.Match;
import pers.ht.mbpo.bean.User;
import pers.ht.mbpo.dao.CompetitorDao;
import pers.ht.mbpo.dao.MatchDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/MatchServlet")
public class MatchServlet extends HttpServlet {
    private MatchDao matchDao = new MatchDao();
    private CompetitorDao competitorDao = new CompetitorDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");
        if ("insert".equals(action)) {
            String matchId = request.getParameter("matchId");
            String matchName = request.getParameter("matchName");
            String regTime = request.getParameter("regTime");
            String beginTime = regTime.substring(0, 10);
            String endTime = regTime.substring(13);
            int maxNumber = Integer.parseInt(request.getParameter("maxNumber"));
            matchDao.insert(new Match(matchId, matchName, beginTime, endTime, maxNumber));
            response.sendRedirect("MatchServlet?action=query");
        }
        if ("update".equals(action)) {
            String matchId = request.getParameter("matchId");
            String matchName = request.getParameter("matchName");
            String regTime = request.getParameter("regTime");
            String beginTime = regTime.substring(0, 10);
            String endTime = regTime.substring(13);
            int maxNumber = Integer.parseInt(request.getParameter("maxNumber"));
            matchDao.edit(new Match(matchId, matchName, beginTime, endTime, maxNumber));
            response.sendRedirect("MatchServlet?action=query");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if ("query".equals(action)) {
            List<Match> matchs = matchDao.query();
            String matchJson = JSONArray.fromObject(matchs).toString();
            request.setAttribute("matchJson", matchJson);
            request.getRequestDispatcher("admin/match.jsp").forward(request, response);
        }
        if ("delete".equals(action)) {
            String matchId = request.getParameter("matchId");
            matchDao.delete(matchId);
            response.sendRedirect("MatchServlet?action=query");
        }
        if ("lock".equals(action)) {
            String matchId = request.getParameter("matchId");
            matchDao.lock(matchId);
            response.sendRedirect("MatchServlet?action=query");
        }
        if ("unLock".equals(action)) {
            String matchId = request.getParameter("matchId");
            matchDao.unLock(matchId);
            response.sendRedirect("MatchServlet?action=query");
        }
        if ("find".equals(action)) {
            String matchId = request.getParameter("matchId");
            Match match = matchDao.queryOne(matchId);
            String regTime = match.getBeginTime() + " - " + match.getEndTime();
            request.setAttribute("match", match);
            request.setAttribute("regTime", regTime);
            request.setAttribute("flag", true);
            request.getRequestDispatcher("admin/AEMatch.jsp").forward(request, response);
        }
        if ("detail".equals(action)) {
            String matchId = request.getParameter("matchId");
            List<User> users = competitorDao.queryUser(matchId);
            String userJson = JSONArray.fromObject(users).toString();
            request.setAttribute("userJson", userJson);
            request.getRequestDispatcher("admin/competitor.jsp").forward(request, response);
        }
        if ("add".equals(action)) {
            request.getRequestDispatcher("admin/AEMatch.jsp").forward(request, response);
        }
    }
}
